package com.wxl.common.util

import android.content.Context
import io.reactivex.Observable
import io.reactivex.ObservableEmitter
import io.reactivex.ObservableOnSubscribe
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.functions.Consumer
import io.reactivex.schedulers.Schedulers
import java.io.InputStream

/**
 * create file time : 2021/3/31
 * create user : wxl
 * subscribe :
 */
class LocalDBServer {

    companion object {

        fun readJobType(context: Context, onNext: Consumer<String>): Disposable {
            return Observable.create(object : ObservableOnSubscribe<String> {
                override fun subscribe(e: ObservableEmitter<String>) {
                    val stream = context.assets.open("job_type.json")
                    val string = inputStream2String(stream)
                    string?.let { e.onNext(it) }
                }
            }).subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(onNext)
        }


       private fun inputStream2String(input: InputStream): String? {
            val out = StringBuffer()
            val b = ByteArray(4096)
            var n: Int
            while (input.read(b).also { n = it } != -1) {
                out.append(String(b, 0, n))
            }
           input.close()
            return out.toString()
        }
    }
}